package com.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.entity.Qishou;
import com.example.dao.QishouDao;
import com.example.service.QishouService;
import com.example.util.HttpClientUtil;
import com.example.util.ResponseCode;
import com.example.util.ResponseData;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (Qishou)表服务实现类
 *
 * @author makejava
 * @since 2023-06-26 19:36:22
 */
@Service("qishouService")
public class QishouServiceImpl implements QishouService {
    @Resource
    private QishouDao qishouDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Qishou queryById(Integer id) {
        return this.qishouDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param qishou 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Qishou> queryByPage(Qishou qishou, PageRequest pageRequest) {
        long total = this.qishouDao.count(qishou);
        return new PageImpl<>(this.qishouDao.queryAllByLimit(qishou, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param qishou 实例对象
     * @return 实例对象
     */
    @Override
    public Qishou insert(Qishou qishou) {
        this.qishouDao.insert(qishou);
        return qishou;
    }

    /**
     * 修改数据
     *
     * @param qishou 实例对象
     * @return 实例对象
     */
    @Override
    public Qishou update(Qishou qishou) {
        this.qishouDao.update(qishou);
        return this.queryById(qishou.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.qishouDao.deleteById(id) > 0;
    }

    @Override
    public ResponseData toRegister(Qishou qishou) {
        String username = qishou.getUsername();
        if(username==null||"".equals(username)){
            return new ResponseData(ResponseCode.FAIL03);
        }
        if(qishou.getPassword()==null||"".equals(qishou.getPassword())){
            return new ResponseData(ResponseCode.FAIL04);
        }
        if(qishou.getPhone()==null||"".equals(qishou.getPhone())){
            return new ResponseData(ResponseCode.FAIL05);
        }

        //查询用户是否被注册user1
        Qishou user1 = qishouDao.queryQishouByUsername(username);
        if (user1 !=null){
            return new ResponseData(ResponseCode.FAIL06);
        }
        //md5密文转换
        String password1 = new Md5Hash(qishou.getPassword(), "cqjtu", 10).toString();
        qishou.setPassword(password1);
        int i=qishouDao.insert(qishou);
        if(i!=1)
        {
            return new ResponseData(ResponseCode.FAIL07);
        }
        return new ResponseData(ResponseCode.SUCCESS02);
    }

    @Override
    public ResponseData toLogin(Qishou qishou, String code) {
        if(qishou.getUsername()==""||qishou.getUsername()==null){
            return new ResponseData(ResponseCode.FAIL03);
        }
        if(qishou.getPassword()==""||qishou.getPassword()==""){
            return new ResponseData(ResponseCode.FAIL04);
        }
        //先查人
        //查询用户名
        Qishou userByUsername = qishouDao.queryQishouByUsername(qishou.getUsername());
        if (userByUsername==null){
            return new ResponseData(ResponseCode.FAIL08);
        }
        //密码字符转换为密文：将用户输入密码转换为密文
        String newPassword = new Md5Hash(qishou.getPassword(), "cqjtu", 10).toString();

        //密码判断：对用户输入密码转换好的密文与从数据库获取的密文进行对比
        if(!userByUsername.getPassword().equals(newPassword))
        {
            return new ResponseData(ResponseCode.FAIL08);
        }
        String url="https://api.weixin.qq.com/sns/jscode2session?appid=wxaacef3d655af1ef8&secret=189e2f415a76322b9e5bad8e9a10d87c&js_code="+code+"&grant_type=authorization_code";
        //对url发起get请求
        String result = HttpClientUtil.doGet(url);
        //对结果进行解析
        JSONObject parse = (JSONObject)JSONObject.parse(result);
        String session_key = (String) parse.get("session_key");
        String openid = (String)parse.get("openid");
        String token=new Md5Hash(session_key,openid).toString();
        userByUsername.setToken(token);
        userByUsername.setOpenid(openid);
        userByUsername.setSessionkey(session_key);
        int i=qishouDao.update(userByUsername);
        if(i!=1){
            return new ResponseData(ResponseCode.FAIL08);
        }
        return new ResponseData(ResponseCode.SUCCESS03,token);
    }
}
